#include <stdio.h>
#include <vector>
using namespace std;

enum CubeSide
{
	cs1,
	cs2,
	cs3,
	cs4,
	cs5,
	cs6,
};

struct Cube
{
	CubeSide L, R, T, N, B, F;

	Cube() {}
	Cube( CubeSide l, CubeSide r, CubeSide t, CubeSide n, CubeSide b, CubeSide f )
		: L(l), R(r), T(t), N(n), B(b), F(f)
	{
	}

	void RotateTB_CW()
	{
		CubeSide tmp = L;
		L = N; N = R; R = F; F = tmp;
	}
	void RotateTB_CCW()
	{
		CubeSide tmp = L;
		L = F; F = R; R = N; N = tmp;
	}

	void RotateTB_twice()
	{
		CubeSide tmp = L; L = R; R = L;
		tmp = N; N = F; F = N;
	}

	void RotateRL_CW()
	{
		CubeSide tmp = T;
		T = N; N = B; B = F; F = tmp;
	}
	void RotateRL_CCW()
	{
		CubeSide tmp = T;
		T = F; F = B; B = N; N = tmp;
	}

	void RotateRL_twice()
	{
		CubeSide tmp = T; T = B; B = T;
		tmp = N; N = F; F = N;
	}

	void RotateNF_CW()
	{
		CubeSide tmp = L;
		L = B; B = R; R = T; T = tmp;
	}
	void RotateNF_CCW()
	{
		CubeSide tmp = L;
		L = T; T = R; R = B; B = tmp;
	}

	void RotateNF_twice()
	{
		CubeSide tmp = R; R = L; L = R;
		tmp = T; T = B; B = T;
	}
};

void SetInitialPosition( Cube& c )
{
	if( c.L == cs1 )      c.RotateTB_CCW();
	else if( c.R == cs1 ) c.RotateTB_CW();
	else if( c.T == cs1 ) c.RotateRL_CCW();
	else if( c.B == cs1 ) c.RotateRL_CW();
	else if( c.F == cs1 ) c.RotateRL_twice();
}

void RotateSideUp( Cube& c, CubeSide side )
{
	if( c.L == side ) c.RotateNF_CW();
	else if( c.R == side ) c.RotateNF_CCW();
	else if( c.B == side ) c.RotateNF_twice();
}

int DetermineScheme_2( Cube& c )
{
	RotateSideUp( c, cs3 );
	switch( c.B )
	{
	case cs4: return (c.L == cs5) ? 0 : 1;
	case cs5: return (c.L == cs6) ? 2 : 3;
	case cs6: return (c.L == cs4) ? 4 : 5;
	default: return -1;
	}
}

int DetermineScheme_3( Cube& c )
{
	RotateSideUp( c, cs2 );
	switch( c.B )
	{
	case cs4: return (c.L == cs5) ? 6 : 7;
	case cs5: return (c.L == cs6) ? 8 : 9;
	case cs6: return (c.L == cs4) ? 10 : 11;
	default: return -1;
	}
}

int DetermineScheme_4( Cube& c )
{
	RotateSideUp( c, cs2 );
	switch( c.B )
	{
	case cs3: return (c.L == cs5) ? 12 : 13;
	case cs5: return (c.L == cs6) ? 14 : 15;
	case cs6: return (c.L == cs5) ? 16 : 17;
	default: return -1;
	}
}

int DetermineScheme_5( Cube& c )
{
	RotateSideUp( c, cs2 );
	switch( c.B )
	{
	case cs3: return (c.L == cs4) ? 18 : 19;
	case cs4: return (c.L == cs3) ? 20 : 21;
	case cs6: return (c.L == cs3) ? 22 : 23;
	default: return -1;
	}
}

int DetermineScheme_6( Cube& c )
{
	RotateSideUp( c, cs2 );
	switch( c.B )
	{
	case cs3: return (c.L == cs4) ? 24 : 25;
	case cs4: return (c.L == cs5) ? 26 : 27;
	case cs5: return (c.L == cs4) ? 28 : 29;
	default: return -1;
	}
}

int DetermineScheme( Cube& c )
{
	SetInitialPosition( c );
	switch( c.F )
	{
	case cs2: return DetermineScheme_2( c );
	case cs3: return DetermineScheme_3( c );
	case cs4: return DetermineScheme_4( c );
	case cs5: return DetermineScheme_5( c );
	case cs6: return DetermineScheme_6( c );
	}
	return -1;
}

int main()
{
	const unsigned int MAX_SCHEMES_COUNT = 30; 
	//vector<unsigned int> schemes[ MAX_SCHEMES_COUNT ];
	vector< vector<unsigned int> > schemes( MAX_SCHEMES_COUNT );

	int N = 0;
	scanf( "%d", &N );

	Cube c;
	for( unsigned int i = 1; i <= N; ++i )
	{
		scanf( "%d %d %d %d %d %d", 
			(int*)&c.L, (int*)&c.R, (int*)&c.T, (int*)&c.N, (int*)&c.B, (int*)&c.F );
		int scheme = DetermineScheme( c );
		schemes[ scheme ].push_back( i );
	}

	//
	int schemes_cnt = 0;
	vector<unsigned int> schemes_indices;
	for( unsigned int i = schemes.size()-1; i >= 0; --i )
		if( schemes[i].size() != 0 )
		{
			++schemes_cnt;
			schemes_indices.push_back( i );

		}

	printf( "%d\n", schemes_cnt );

	while( schemes_indices.size() )
	{
		// Ищем схему с минимальным номером кости
		unsigned int min = 0;
		unsigned int min_cube_num = schemes[ schemes_indices[0] ][0];
		const unsigned int cnt = schemes_indices.size();
		for( unsigned int i = 0; i < cnt; ++i )
		{
			const unsigned int num = schemes[ schemes_indices[i] ][0];
			if( num < min_cube_num )
			{
				min_cube_num = num;
				min = i;
			}
		}

		vector<unsigned int>& s = schemes[ min ];
		const unsigned int cubes_cnt = s.size();
		for( int i = 0; i < (cubes_cnt-1); ++i )
			printf( "%d ", s[i] );
		printf( "%d\n", s[cubes_cnt-1] );

		schemes_indices.
	}

	return 0;
}

